package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: IndexController
 * Package: com.atguigu.spzx.manager.controller
 * Description:
 *
 * @Author 刘强
 * @Create 2024/6/27 15:52
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/system/index")
@Tag(name = "首页接口")
public class IndexController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private ValidateCodeService validateCodeService;
    @Resource
    private SysMenuService sysMenuService;
    /**
     * 根据用户信息查询菜单
     * @return
     */
    @GetMapping("/menus")
    public Result muns(){
        List<SysMenuVo> list =  sysMenuService.findUserMenuList();
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }

    /**
     * 退出登陆
     * @param token
     * @return
     */
    @GetMapping("logout")
    public Result logout(@RequestHeader("token") String token){
       sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("token") String token){
        //获取用户信息  ---> 因为在拦截器中进行用户的认证，所以顺道将用户信息保存到了线程本地变量中了 ---就可以不用查询redis了
//        SysUser sysUser = sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.get();
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/generateValidateCode")
    public Result generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登陆
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
}
